package cn.zjyj.module.zjyj.service.risklevelconf;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.zjyj.module.zjyj.controller.admin.risklevelconf.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.risklevelconf.RiskLevelConfDO;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;

import cn.zjyj.module.zjyj.dal.mysql.risklevelconf.RiskLevelConfMapper;

import static cn.zjyj.framework.common.common.exception.util.ServiceExceptionUtil.exception;
import static cn.zjyj.framework.common.common.util.collection.CollectionUtils.convertList;
import static cn.zjyj.module.zjyj.enums.ErrorCodeConstants.*;

/**
 * 风险等级配置 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class RiskLevelConfServiceImpl implements RiskLevelConfService {

    @Resource
    private RiskLevelConfMapper riskLevelConfMapper;

    @Override
    public Long createRiskLevelConf(RiskLevelConfSaveReqVO createReqVO) {
        // 插入
        RiskLevelConfDO riskLevelConf = BeanUtils.toBean(createReqVO, RiskLevelConfDO.class);
        riskLevelConfMapper.insert(riskLevelConf);
        // 返回
        return riskLevelConf.getId();
    }

    @Override
    public void updateRiskLevelConf(RiskLevelConfSaveReqVO updateReqVO) {
        // 校验存在
        validateRiskLevelConfExists(updateReqVO.getId());
        // 更新
        RiskLevelConfDO updateObj = BeanUtils.toBean(updateReqVO, RiskLevelConfDO.class);
        riskLevelConfMapper.updateById(updateObj);
    }

    @Override
    public void deleteRiskLevelConf(Long id) {
        // 校验存在
        validateRiskLevelConfExists(id);
        // 删除
        riskLevelConfMapper.deleteById(id);
    }

    @Override
        public void deleteRiskLevelConfListByIds(List<Long> ids) {
        // 校验存在
        validateRiskLevelConfExists(ids);
        // 删除
        riskLevelConfMapper.deleteByIds(ids);
        }

    private void validateRiskLevelConfExists(List<Long> ids) {
        List<RiskLevelConfDO> list = riskLevelConfMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RISK_LEVEL_CONF_NOT_EXISTS);
        }
    }

    private void validateRiskLevelConfExists(Long id) {
        if (riskLevelConfMapper.selectById(id) == null) {
            throw exception(RISK_LEVEL_CONF_NOT_EXISTS);
        }
    }

    @Override
    public RiskLevelConfDO getRiskLevelConf(Long id) {
        return riskLevelConfMapper.selectById(id);
    }

    @Override
    public PageResult<RiskLevelConfDO> getRiskLevelConfPage(RiskLevelConfPageReqVO pageReqVO) {
        return riskLevelConfMapper.selectPage(pageReqVO);
    }

}